<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :disabled="ifdisabled" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="主键" prop="createTime" v-if="show">
        <el-input v-model="dataForm.travelId" placeholder="主键"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="show">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime" v-if="show">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="删除标识" prop="delFlag" v-if="show">
        <el-input v-model="dataForm.delFlag" placeholder="删除标识"></el-input>
      </el-form-item>
      <el-form-item label="所属租户" prop="tenantId" v-if="show">
        <el-input v-model="dataForm.tenantId" placeholder="所属租户"></el-input>
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标题" prop="title">
            <el-input v-model="dataForm.title" placeholder="标题"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出差天数" prop="days">
            <el-input v-model="dataForm.days" placeholder="出差天数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="申请人" prop="username" v-if="show">
            <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="state" v-if="show">
            <!--<el-input v-model="dataForm.state" placeholder="状态" :disabled="true"></el-input>-->
            <el-select placeholder="请选择" v-model="dataForm.state" clearable :disabled="true" style="width: 100%">
              <el-option
                v-for="item in statusoption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开始时间" prop="starttime">
            <el-date-picker v-model="dataForm.starttime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始时间" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止时间" prop="endtime">
            <el-date-picker v-model="dataForm.endtime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="截止时间" style="width: 100%"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="content">
            <el-input type="textarea" :rows="2" v-model="dataForm.content" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-if="sub" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getObj, addObj, putObj} from '@/api/oatravelbill'

  export default {
    data () {
      return {
        // 状态
        ifdisabled: false,
        sub: true,
        statusoption: [{
          value: 0,
          label: '未提交'
        }, {
          value: 1,
          label: '审核中'
        }, {
          value: 2,
          label: '已完成'
        }, {
          value: 3,
          label: '放弃'
        }, {
          value: 9,
          label: '驳回'
        }, {
          value: 4,
          label: '未知状态'
        }],
        show: false,
        visible: false,
        dataForm: {
          travelId: 0,
          username: '',
          title: '',
          days: '',
          content: '',
          state: 0,
          starttime: '',
          endtime: '',
        },
        dataRule: {
          username: [
            { required: true, message: '申请人不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          days: [
            { required: true, message: '出差天数不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '截止时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,type) {
        this.dataForm.travelId = id || 0
        this.visible = true
        if(type === 'N'){
          this.ifdisabled = true
          this.sub = false
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.travelId) {
            getObj(this.dataForm.travelId).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        // console.log(this.dataForm)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.travelId) {
              putObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>
